#pragma once 

#include<iostream>
#include<pthread.h>
#include"TcpServer.hpp"
#include"Protocol.hpp"
#include"log.hpp"

#define PORT 8888

class HttpServer
{
private:
    int port;
    TcpServer*tcp_server;
    bool stop; //标记服务状态
public:
    HttpServer(int _port=PORT)
    :port(_port),tcp_server(nullptr),stop(false)
    {}

    void InitServer()
    {
        tcp_server = TcpServer::getinstance(port);
    }

    //启动服务
    void Loop()
    {
        LOG(INFO,"Loop begin")
        int listen_sock = tcp_server->Sock();
        while(!stop)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sock = accept(listen_sock ,(struct sockaddr*)&peer,&len);
            if(sock<0)
            {
                continue;
            }
            LOG(INFO,"Get a new link");
            //获取新连接后，交付给线程进行处理
            int *_sock = new int(sock);
            pthread_t tid;
            pthread_create(&tid,nullptr,Entrance::HandlerRequest,_sock);
            pthread_detach(tid);//线程 分离
        }
    }
    ~HttpServer()
    {}
};